package qlsl.androiddesign.adapter.commonadapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;

import qlsl.androiddesign.util.commonutil.Log;

public class ViewPagerFragmentAdapterTitle extends FragmentPagerAdapter {
	private ArrayList<String> tabTitles;
	private ArrayList<FragmentInfo> fragmentInfos;
	private Context context;

	public ViewPagerFragmentAdapterTitle(Context context, FragmentManager fm) {
		super(fm);
		this.context = context;
		fragmentInfos = new ArrayList<FragmentInfo>();
		tabTitles = new ArrayList<String>();
	}

	/**
	 * 添加一页
	 * 
	 * @param title
	 * @param clazz
	 * @param bundle
	 */
	public void addPage(String title, Class<?> clazz, Bundle bundle) {
		tabTitles.add(title);
		fragmentInfos.add(new FragmentInfo(clazz, bundle));
	}

	@Override
	public Fragment getItem(int position) {
		FragmentInfo fragmentInfo = fragmentInfos.get(position);
		Fragment fragment = Fragment.instantiate(context, fragmentInfo
				.getClazz().getName(), fragmentInfo.getBundle());
		return fragment;
	}

	@Override
	public int getCount() {
		return tabTitles.size();
	}

	@Override
	public CharSequence getPageTitle(int position) {
		return tabTitles.get(position);
	}

	/**
	 * 封装每一页的fragment的信息
	 * 
	 * @author Administrator
	 * 
	 */
	public class FragmentInfo {
		private Class<?> clazz;
		private Bundle bundle;

		public FragmentInfo(Class<?> clazz, Bundle bundle) {
			super();
			this.clazz = clazz;
			this.bundle = bundle;
		}

		public Class<?> getClazz() {
			return clazz;
		}

		public void setClazz(Class<?> clazz) {
			this.clazz = clazz;
		}

		public Bundle getBundle() {
			return bundle;
		}

		public void setBundle(Bundle bundle) {
			this.bundle = bundle;
		}
	}
}
